package com.tanhua.server.controller;

import com.tanhua.model.domain.CommentType;
import com.tanhua.model.domain.PageResult;
import com.tanhua.model.domain.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/messages")
public class MessagesController {

    @Autowired
    private MessagesService messagesService;

    /**
     * 根据环信id查询用户详情
     */
    @GetMapping("/userinfo")
    public ResponseEntity userinfo(String huanxinId) {
        UserInfoVo vo = messagesService.findUserInfoByHuanxin(huanxinId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 添加好友
     */
    @PostMapping("/contacts")
    public ResponseEntity contacts(@RequestBody Map map) {
        String friendId = map.get("userId").toString();
        Long aLong = Long.valueOf(friendId);
        messagesService.contacts(aLong);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询联系人列表
     */
    @GetMapping("/contacts")
    public ResponseEntity contacts(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   String keyword) {
        PageResult pageResult = messagesService.findFriends(page,pagesize,keyword);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 喜欢列表
     */
    @GetMapping("/loves")
    public ResponseEntity loveList(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = messagesService.findLoves(page,pagesize, UserHolder.getUserId(), CommentType.LOVE);
        return ResponseEntity.ok(pageResult);
    }
    /**
     * 点赞列表
     */
    @GetMapping("/likes")
    public ResponseEntity likeList(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = messagesService.findLoves(page,pagesize, UserHolder.getUserId(), CommentType.LIKE);
        return ResponseEntity.ok(pageResult);
    }
    /**
     * 评论列表
     */
    @GetMapping("/comments")
    public ResponseEntity commentList(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = messagesService.findLoves(page,pagesize, UserHolder.getUserId(), CommentType.COMMENT);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询公告
     */
    @GetMapping("/announcements")
    public ResponseEntity findByAnnouncement(@RequestParam(defaultValue = "1") Integer page ,@RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult  = messagesService.findAnnouncements(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }
}